home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11060 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.sinet.slb.com!usenet
  2. From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: "Deep" Destructor Query
  5. Date: Tue, 12 Mar 1996 08:22:17 -0600
  6. Organization: Schlumberger Anadrill
  7. Message-ID: <31458899.6A44@sugar-land.anadrill.slb.com>
  8. References: <826257180snz@pbutler.demon.co.uk>
  9. NNTP-Posting-Host: 163.185.118.40
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Peter Hugh Butler wrote:
  16. > Object::~Object()
  17. > {
  18. > for (int i = 0; i < numshapes; i++)
  19. >         shapes[i].~shape();  //"Deep" destructor, deletes underlying arrays.
  20. > delete [] shapes;  //delete the array of pointers
  21. > }
  22. > The program causes a General Protection Fault (guess which OS I'm using and
  23. > win a cookie).  It fails when it tries to delete the array of null pointers
  24. > pointed to by shape.  It works when I leave the "delete [] shapes;" line out.
  25. > Well, I have two questions:
  26. > 1) If I simply "delete [] shapes" with no loop to delete the arrays underneath,
  27. >    will the ~shape() destructor be automatically called, thus deleting the
  28. >    shape objects correctly?  I wouldn't think so, but it would be good to be
  29. >    corrected.
  30.  
  31. Just deleting the array of pointers will not destroy the objects pointed at. You
  32. must explicitly delete all the shape objects as you attempted to do in the code listing
  33. above. However, the correct way to do it is not to call the shape object's destructor 
  34. explicitly but to use the delete operator as follows:
  35.  
  36. Object::~Object()
  37. {
  38. for (int i = 0; i < numshapes; i++)
  39.         delete shapes[i];    // delete the dynamically allocated shape objects
  40.  
  41. delete [] shapes;  //delete the array of pointers
  42. }
  43.  
  44. The delete operator in turn will call the shape object's destructor before deleting
  45. the shape object. 
  46.  
  47. The problem with calling the destructor directly is that the object's content, in this
  48. case the point array, will be destroyed but not the object itself. To destroy the object,
  49. you'll have to use the delete operator. As a rule-of-thumb, 
  50. you should never call the destructor yourself. Well, there are a few cases in which 
  51. you can do that but that is extremely rare. In those cases, you can always write a
  52. cleanup helper function which is called both the destructor and where you want to
  53. clean up the object.
  54.  
  55. -- 
  56. --------------------------------------------------------------------------
  57. * Vinh Nguyen                                            vnguyen@slb.com *
  58. * Drilling Information Products - Senior Engineer                        *
  59. * Anadrill Schlumberger                             *
  60. * 200 Gillingham Ln.                             (713) 275-7524 (Office) *
  61. * Sugarland, TX 77478                            (713) 275-8098 (FAX)    *
  62. --------------------------------------------------------------------------
  63.